﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ArduinoSketch.Core
{
    public partial class frmSolution : BaseFormContent, ISolutionExplorer
    {

        private TreeNode _curProjectNode;
        private TreeNode _curNodeSelected;

       
        public frmSolution()
        {
            InitializeComponent();

            treeViewProject.ExpandAll();
        }

        #region ISolutionExplorer Members

        public void ClearSolution()
        {
            this.treeViewProject.Nodes.Clear();
            this.propertyGridDetails.SelectedObject = null;
        }

        public void CreateSolutionView(SolutionEntity solution)
        {
            string nodeName = "Solution '" + solution.SolutionName + "' " + "(" + solution.Projects.Count.ToString() + " projects)";
            TreeNode _root = new TreeNode(nodeName);
            _root.Tag = solution;
            foreach (var objProject in solution.Projects)
            {
                TreeNode projectNode = new TreeNode(objProject.ProjectName);
                projectNode.ImageIndex = Directory.Exists(objProject.ProjectPath) ? 2 : 4;
                projectNode.SelectedImageIndex = Directory.Exists(objProject.ProjectPath) ? 2 : 4;
                _root.Nodes.Add(projectNode);
                projectNode.Tag = objProject;
                foreach (var objFile in objProject.ProjectFiles)
                {
                    string _fullName = string.Format(@"{0}\{1}", objFile.FilePath, objFile.FileName);
                    TreeNode fileNode = new TreeNode(objFile.FileName);
                    fileNode.ImageIndex = File.Exists(_fullName) ? 3 : 5;
                    fileNode.SelectedImageIndex = File.Exists(_fullName) ? 3 : 5;
                    fileNode.Tag = objFile;
                    projectNode.Nodes.Add(fileNode);
                    objFile.Project = objProject;
                }
            }

            this.treeViewProject.Nodes.Clear();
            this.treeViewProject.Nodes.Add(_root);
            this.treeViewProject.ExpandAll();
        }

        public void CreateProjectView(ProjectEntity project)
        {
           
            TreeNode _root = this.treeViewProject.Nodes[0];

            TreeNode projectNode = new TreeNode(project.ProjectName);

            projectNode.ImageIndex = Directory.Exists(project.ProjectPath) ? 2 : 4;
            projectNode.SelectedImageIndex = Directory.Exists(project.ProjectPath) ? 2 : 4;
            _root.Nodes.Add(projectNode);
            projectNode.Tag = project;
            foreach (var objFile in project.ProjectFiles)
            {
                string _fullName = string.Format(@"{0}\{1}", objFile.FilePath, objFile.FileName);
                TreeNode fileNode = new TreeNode(objFile.FileName);
                fileNode.ImageIndex = File.Exists(_fullName) ? 3 : 5;
                fileNode.SelectedImageIndex = File.Exists(_fullName) ? 3 : 5;
                fileNode.Tag = objFile;
                projectNode.Nodes.Add(fileNode);
                objFile.Project = project;
            }
            SolutionEntity solution = (SolutionEntity)_root.Tag;
            string nodeName = "Solution '" + solution.SolutionName + "' " + "(" + solution.Projects.Count.ToString() + " projects)";
            _root.Text = nodeName;
            projectNode.ExpandAll();
        }

        public void CreateFileView(ProjectFileEntity file)
        {
            TreeNode fileNode = new TreeNode(file.FileName);
            fileNode.ImageIndex = 3;
            fileNode.SelectedImageIndex = 3;
            fileNode.Tag = file;
            _curProjectNode.Nodes.Add(fileNode);
        }

        public event EventHandler<ProjectFileArgs> OnExplorerSelected;

        public event EventHandler<ProjectSelectedArgs> OnProjectSelected;

        #endregion

        private void treeViewProject_AfterSelect(object sender, TreeViewEventArgs e)
        {
           
            propertyGridDetails.SelectedObject = null;
            _curProjectNode = null; 
            if (e.Node.Tag is ProjectFileEntity)
            {
                ProjectFileEntity _projectFile = (ProjectFileEntity)e.Node.Tag;
                propertyGridDetails.SelectedObject = _projectFile;
            }

            if (e.Node.Tag is ProjectEntity)
            {
                ProjectEntity _projectFile = (ProjectEntity)e.Node.Tag;
                _curProjectNode = e.Node;
                propertyGridDetails.SelectedObject = _projectFile;
                if (OnProjectSelected != null)
                {
                    ProjectSelectedArgs _args = new ProjectSelectedArgs(_projectFile);
                    OnProjectSelected(this, _args);
                }
            }

            _curNodeSelected = e.Node;
        }

        private void treeViewProject_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
        {

        }

        private void treeViewProject_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node.Tag is ProjectFileEntity)
            {
                if (OnExplorerSelected != null)
                {
                    ProjectFileArgs _args = new ProjectFileArgs((ProjectFileEntity)e.Node.Tag);
                    OnExplorerSelected(this, _args);
                }
            }
        }

        private void treeViewProject_DrawNode(object sender, DrawTreeNodeEventArgs e)
        {
            if (e.Node.Tag is SolutionEntity)
            {
                e.Node.ImageIndex = 0;
                e.Node.StateImageIndex = 0;
            }

            if (e.Node.Tag is ProjectEntity)
            {
                e.Node.ImageIndex = 2;
                e.Node.StateImageIndex = 2;
            }

            if (e.Node.Tag is ProjectFileEntity)
            {
                e.Node.ImageIndex = 3;
                e.Node.StateImageIndex = 3;
            }
        }

        private void ToolMenuStatus(int x, int y)
        {
            this.SetToolMenuVisible();

            if (_curNodeSelected != null)
            {
                if (_curNodeSelected.Tag is SolutionEntity)
                {
                    toolNewProject.Visible = true;
                    toolExistingProject.Visible = true;
                }
                else if (_curNodeSelected.Tag is ProjectEntity)
                {
                    toolHeaderFile.Visible = true;
                    toolSourceFile.Visible = true;
                    toolExternalFile.Visible = true;
                    toolRemoveProject.Visible = true;
                }
                else if (_curNodeSelected.Tag is ProjectFileEntity)
                {
                    toolRemoveFile.Visible = true;
                    toolOpenFile.Visible = true;
                }
                
            }
            else if (treeViewProject.Nodes.Count == 0)
            {
                toolAddSulotion.Visible = true;
            }

            treeContextMenu.Show(treeViewProject, x, y);
        }

        private void SetToolMenuVisible()
        {
            toolAddSulotion.Visible = false;
            toolNewProject.Visible = false;
            toolExistingProject.Visible = false;
            toolHeaderFile.Visible = false;
            toolSourceFile.Visible = false;
            toolExternalFile.Visible = false;
            toolRemoveFile.Visible = false;
            toolRemoveProject.Visible = false;
            toolOpenFile.Visible = false;
        }


        private void treeViewProject_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                ToolMenuStatus(e.X, e.Y);
            }
        }

        public void RemoveSelectedNode()
        {
            this.treeViewProject.Nodes.Remove(_curNodeSelected);
            _curNodeSelected = null;
        }

        public string SelectedFileName 
        {
            get
            {
                string _returnValue = String.Empty;
                if ((_curNodeSelected != null) && (_curNodeSelected.Tag is ProjectFileEntity))
                {
                    ProjectFileEntity _projectFile = (ProjectFileEntity)_curNodeSelected.Tag;
                    _returnValue = _projectFile.FileName;
                }

                return _returnValue;
            }
        }

        #region Event Handler

        public event EventHandler OnToolNewSolutionClick;
        public event EventHandler OnToolNewProjectClick;
        public event EventHandler OnToolExistingProjectClick;
        public event EventHandler OnToolNewHeaderFileClick;
        public event EventHandler OnToolNewSourceFileClick;
        public event EventHandler OnToolNewExternalFileClick;
        public event EventHandler OnToolRemoveFileClick;
        public event EventHandler OnToolRemoveProjectClick;

        private void toolAddSulotion_Click(object sender, EventArgs e)
        {
            if (OnToolNewSolutionClick != null)
                OnToolNewSolutionClick(this, EventArgs.Empty);
        }

        private void toolNewProject_Click(object sender, EventArgs e)
        {
            if (OnToolNewProjectClick != null)
                OnToolNewProjectClick(this, EventArgs.Empty);
        }

        private void toolHeaderFile_Click(object sender, EventArgs e)
        {
            if (OnToolNewHeaderFileClick != null)
                OnToolNewHeaderFileClick(this, EventArgs.Empty);
        }

        private void toolSourceFile_Click(object sender, EventArgs e)
        {
            if (OnToolNewSourceFileClick != null)
                OnToolNewSourceFileClick(this, EventArgs.Empty);
        }

        private void toolExternalFile_Click(object sender, EventArgs e)
        {
            if (OnToolNewExternalFileClick != null)
                OnToolNewExternalFileClick(this, EventArgs.Empty);
        }

        private void toolRemoveFile_Click(object sender, EventArgs e)
        {
            if (OnToolRemoveFileClick != null)
                OnToolRemoveFileClick(this, EventArgs.Empty);
        }

        private void toolRemoveProject_Click(object sender, EventArgs e)
        {
            if (OnToolRemoveProjectClick != null)
                OnToolRemoveProjectClick(this, EventArgs.Empty);
        }

        private void toolExistingProject_Click(object sender, EventArgs e)
        {
            if (OnToolExistingProjectClick != null)
                OnToolExistingProjectClick(this, EventArgs.Empty);
        }
        #endregion
       
    }
}
